/*
 * DocsOpenGoogleDocsAction.java
 * 
 * Copyright (c) 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.dcarew.googledocs.actions;

import org.dcarew.googledocs.DocsPlugin;
import org.dcarew.googledocs.dialogs.DocsOpenDocDialog;
import org.dcarew.googledocs.editor.DocsEditorInput;
import org.dcarew.googledocs.editor.SpreadsheetEditor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PartInitException;

import com.google.gdata.data.spreadsheet.SpreadsheetEntry;

/**
 * Our sample action implements workbench action delegate. The action proxy will be created by the
 * workbench and shown in the UI. When the user tries to use the action, this delegate will be
 * created and execution will be delegated to it.
 * 
 * @see IWorkbenchWindowActionDelegate
 */
public class DocsOpenGoogleSpreadsheetAction
	implements IWorkbenchWindowActionDelegate
{
	private IWorkbenchWindow window;
	
	
	/**
	 * The constructor.
	 */
	public DocsOpenGoogleSpreadsheetAction()
	{
		
	}

	/**
	 * The action has been activated. The argument of the method represents the 'real' action
	 * sitting in the workbench UI.
	 * 
	 * @see IWorkbenchWindowActionDelegate#run
	 */
	public void run(IAction action)
	{
		DocsOpenDocDialog dialog = new DocsOpenDocDialog(window.getShell(), true);
		
		if (dialog.open() == Window.OK && dialog.getResultAsSpreadSheet() != null)
		{
			SpreadsheetEntry spreadsheet = dialog.getResultAsSpreadSheet();
			
			DocsEditorInput editorInput = new DocsEditorInput(spreadsheet);
			
			try
			{
				window.getActivePage().openEditor(editorInput, SpreadsheetEditor.EDITOR_ID, true, IWorkbenchPage.MATCH_INPUT);
			}
			catch (PartInitException exception)
			{
				ErrorDialog.openError(window.getShell(),
					"Error Opening Spreadsheet",
					"Unable to open " + spreadsheet.getTitle().getPlainText() + ".\n"
						+ exception.toString(),
					new Status(IStatus.ERROR, DocsPlugin.PLUGIN_ID, exception.getMessage(), exception));
				
				DocsPlugin.log(exception);
			}			
		}
	}
	
	public void init(IWorkbenchWindow window)
	{
		this.window = window;
	}
	
	/**
	 * Selection in the workbench has been changed. We can change the state of the 'real' action
	 * here if we want, but this can only happen after the delegate has been created.
	 * 
	 * @see IWorkbenchWindowActionDelegate#selectionChanged
	 */
	public void selectionChanged(IAction action, ISelection selection)
	{
		
	}

	public void dispose()
	{
		
	}
	
}
